/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { coreParameter } from '../../data/global-core-parameters.js';
import { WorkerPoolType } from '../enum/worker-pool-type.js';

import { PoolConfig } from './pool-config.js';
import { WorkerPool } from './worker-pool.js';
import { WorkerPoolImpl } from './worker-pool-impl.js';

/**
 * 工作池工厂
 * 创建不同类型的工作池
 *
 * @since 2022/8/17
 */
export class WorkerPoolFactory {
  static getWorkerPool(type: WorkerPoolType, poolConfig: PoolConfig): WorkerPool | undefined {
    if (type === WorkerPoolType.LOCAL) {
      return WorkerPoolImpl.getInstance(poolConfig);
    }
    return undefined;
  }

  static getDefaultWorkerPool(): WorkerPool {
    return this.getWorkerPool(WorkerPoolType.LOCAL, {
      minPoolNum: 2,
      maxPoolNum: coreParameter.properties.hvigorPoolMaxSize,
      maxCoreSize: coreParameter.properties.hvigorPoolMaxCoreSize,
      cacheCapacity: coreParameter.properties.hvigorPoolCacheCapacity,
      cacheTtl: coreParameter.properties.hvigorPoolCacheTtl,
    })!;
  }
}
